﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using ThietBiSo.LinQDB;
using ThietBiSo.Models;

using System.Web.Mvc;
using System.Web.Script.Services;
using ThietBiSo.Common;

using System.IO;

namespace ThietBiSo
{
    /// <summary>
    /// Summary description for WebService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    [System.Web.Script.Services.ScriptService]
    public class WebService : System.Web.Services.WebService
    {


       
        private MOnline monl = new MOnline();

      
        private MQuangCao mqc = new MQuangCao();

        private MSlider msl = new MSlider();

        private MLogo mlg = new MLogo();
     
        MAdmin mad = new MAdmin();
        Admin ad = new Admin();

        //MBaiViet mbv = new MBaiViet();


        private MTinTuc mtt = new MTinTuc();


        private MFormLienHe mflh = new MFormLienHe();
        private FormLienHe flh = new FormLienHe();

        private MBanner mbn = new MBanner();



        _24hmuaonlineDataContext context = new _24hmuaonlineDataContext();


       


        //==========ONline=================
        [HttpPost]
        [WebMethod]
        [ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
        public void Online_Delete(int id)
        {
            monl.Delete(id);
        }
        [HttpPost]
        [WebMethod]
        [ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
        public void Online_TrangChu(int id)
        {
            monl.HienThi(id);
        }

        //==============End ONLINE=========================



        //----------TIN TỨC-----------
        [HttpPost]
        [WebMethod]
        [ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
        public void TinTuc_TinMoi(int id)
        {
            mtt.TinMoi(id);
        }
        [HttpPost]
        [WebMethod]
        [ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
        public void TinTuc_HienThi(int id)
        {
            mtt.HienThi(id);
        }


        [HttpPost]
        [WebMethod]
        [ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
        public void TinTuc_Delete(int id)
        {
            mtt.Delete(id);

        }

     

     

        //----------QUANG CÁO-----------
        [HttpPost]
        [WebMethod]
        [ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
        public void QuangCao_HienThi(int id)
        {
            mqc.HienThi(id);
        }


        [HttpPost]
        [WebMethod]
        [ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
        public void QuangCao_Delete(int id)
        {

            mqc.Delete(id);

        }



        //================REGISTER=thanh vien======================      
        private bool CheckEmail(string email)
        {
            System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
            if (!string.IsNullOrEmpty(email))
            {
                string[] emaillist = email.Split(new char[] { ',', ';' });
                int countEmailOk = 0;
                foreach (var e in emaillist)
                {
                    if (regex.IsMatch(e)) ++countEmailOk;
                }
                if (countEmailOk == emaillist.Count(ee => !string.IsNullOrEmpty(ee))) return true;
                else return false;
            }
            else return false;
        }
      
     
        //================LOGIN ADMIN=======================


        [ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
        [WebMethod(EnableSession = true)]
        public bool LoginAdmin(string Username, string Password)
        {
           // string cryptorPassword = CryptorEngine.Encrypt(Password, true);
            ad = mad.login(Username, Password);
            if (ad == null)
            {
                return false;

            }
            Session["HoTenAdmin"] = ad;
            return true;
        }


        [ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
        [WebMethod(EnableSession = true)]
        public bool LogoutAdmin()
        {
            Session.Remove("HoTenAdmin");
            return true;

        }




        //================Slider=======================
        [HttpPost]
        [WebMethod]
        [ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
        public void HienThiSlider(int id)
        {
            msl.HienThiSldier(id);
        }
        [HttpPost]
        [WebMethod]
        [ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
        public void DeleteSlider(int id)
        {

            msl.Delete(id);

        }

        //================Logo=======================
        [HttpPost]
        [WebMethod]
        [ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
        public void HienThiLogo(int id)
        {
            mlg.HienThiSldier(id);
        }
        [HttpPost]
        [WebMethod]
        [ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
        public void DeleteLogo(int id)
        {

            mlg.Delete(id);

        }





     
        //==========FORM LIEN HE=================
        [HttpPost]
        [WebMethod]
        [ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
        public void FormLienHe_Delete(int id)
        {
            mflh.Delete(id);
        }

        [WebMethod]
        [ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
        public string FormLienHe(string HoTen_lh, string Email_lh, string SoDienThoai_lh, string DiaChi_lh, string ChiTiet_lh)
        {
            try
            {
                if (CheckEmail(Email_lh))
                {


                    flh.Id = mflh.GetMax() + 1;
                    flh.HoTen = HoTen_lh;
                    flh.Email = Email_lh;

                    flh.DiaChi = DiaChi_lh;
                    flh.SoDienThoai = SoDienThoai_lh;
                    flh.NoiDung = ChiTiet_lh;
                    mflh.Save(flh);
                    return "Gửi thông tin liên hệ thành công";
                }
                else
                {
                    return "Email không đúng!!!";

                }
            }
            catch (Exception ex)
            {
                return "Có lỗi xãy ra " + ex.Message;
            }
        }


        //==========BOX GOI LAI CHO TOI=================

        [WebMethod]
        [ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
        public string FormGoiLaiChoToi(string HoTen_box, string Email_box, string NoiDung_box)
        {
            try
            {
                //if (CheckEmail(Email_box))
                //{


                flh.Id = mflh.GetMax() + 1;
                flh.HoTen = HoTen_box;
                flh.SoDienThoai = Email_box;
                flh.NoiDung = NoiDung_box;
                mflh.Save(flh);
                return "Gửi thông tin thành công";
                //}
                //else
                //{
                //    return "Email không đúng!!!";

                //}
            }
            catch (Exception ex)
            {
                return "Có lỗi xãy ra " + ex.Message;
            }
        }


        //==========BAnner=================
        [HttpPost]
        [WebMethod]
        [ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
        public void DeleteBanner(int id)
        {
            mbn.Delete(id);
        }
        [HttpPost]
        [WebMethod]
        [ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
        public void HienThiBanner(int id)
        {
            mbn.HienThi(id);
        }
      
    }
}
